package com.lzy.webapi.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.OrderProductDto;
import com.lzy.webapi.component.UserToken;
import com.lzy.webapi.facade.OrderAppFacade;
import com.lzy.webapi.request.OrderProductRequest;
import com.lzy.webapi.response.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by WIN7 on 2017/12/15.
 */
@Controller
@RequestMapping("api/app/order")
public class OrderAppController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderAppController.class);

    @Autowired
    private OrderAppFacade orderAppFacade;

    /**
     * 查询产品订单列表
     *
     * @param token
     * @param request
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public Result orderList(
            @RequestHeader("token") String token,
            OrderProductRequest request) {
        Ensure.that(request).isNotNull("04000001");
        UserToken ut = isLogin(token);
        Result result = new Result();
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        OrderProductDto orderProductDto = DozerUtils.map(request, OrderProductDto.class);
        orderProductDto.setMemberId(ut.getUserId());
        orderProductDto.setType(BusinessType.OrderProductType.PRODUCT.getCode());
        PageInfo pageInfo = orderAppFacade.orderProductList(pager, orderProductDto);
        if (pageInfo != null) {
            result.setResult(DozerUtils.maps(pageInfo.getList(), OrderProductListAppResponse.class));
        }
        return result;
    }

    /**
     * 查看产品订单详情
     *
     * @param token
     * @param orderId
     * @return
     */
    @RequestMapping(value = "detail/{orderId}", method = RequestMethod.GET)
    @ResponseBody
    public Result orderProductDetail(@RequestHeader("token") String token, @PathVariable("orderId") Long orderId) {
        Ensure.that(orderId).isNotNull("04000001");
        isLogin(token);
        Result result = new Result();
        OrderProductDetailAppResponse orderProductDetailResponse = orderAppFacade.orderProductDetail(orderId);
        if (orderProductDetailResponse != null) {
            result.setResult(orderProductDetailResponse);
        }
        return result;
    }

    /**
     * 查看产品订单详情
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "getDetail/{orderId}", method = RequestMethod.GET)
    @ResponseBody
    public Result orderProductDetail(@PathVariable("orderId") Long orderId) {
        Ensure.that(orderId).isNotNull("04000001");
        Result result = new Result();
        OrderProductDetailAppResponse orderProductDetailResponse = orderAppFacade.orderProductDetail(orderId);
        if (orderProductDetailResponse != null) {
            result.setResult(orderProductDetailResponse);
        }
        return result;
    }

    /**
     * 查询卡券宝订单列表
     *
     * @param token
     * @param request
     * @return
     */
    @RequestMapping(value = "couponsList", method = RequestMethod.GET)
    @ResponseBody
    public Result orderCouponsList(@RequestHeader("token") String token, OrderProductRequest request) {
        Ensure.that(request).isNotNull("04000001");
        UserToken ut = isLogin(token);
        Result result = new Result();
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        OrderProductDto orderProductDto = DozerUtils.map(request, OrderProductDto.class);
        orderProductDto.setMemberId(ut.getUserId());
        orderProductDto.setType(BusinessType.OrderProductType.COUPONS.getCode());
        PageInfo pageInfo = orderAppFacade.orderCouponsList(pager, orderProductDto);
        if (pageInfo != null) {
            result.setResult(DozerUtils.maps(pageInfo.getList(), OrderCouponsListAppResponse.class));
        }
        return result;
    }

    /**
     * 查询卡券宝订单详情
     *
     * @param token
     * @param orderId
     * @return
     */
    @RequestMapping(value = "couponsDetail/{orderId}", method = RequestMethod.GET)
    @ResponseBody
    public Result orderCouponsDetail(@RequestHeader("token") String token, @PathVariable("orderId") Long orderId) {
        Ensure.that(orderId).isNotNull("04000001");
        isLogin(token);
        Result result = new Result();
        OrderCouponsDetailAppResponse orderCouponsDetailResponse = orderAppFacade.orderCouponsDetail(orderId);
        if (orderCouponsDetailResponse != null) {
            result.setResult(orderCouponsDetailResponse);
        }
        return result;
    }

    /**
     * 查询会员身份以及编号
     *
//     * @param token
     * @param type 1-卡券宝  2-代理销售
     * @return
     */
    @RequestMapping(value = "identity/{type}", method = RequestMethod.GET)
    @ResponseBody
    public Result getIdentity(@RequestHeader("token") String token,@PathVariable("type") Integer type) {
        UserToken ut = isLogin(token);
        MemberIdentityResponse memberIdentityResponse = orderAppFacade.getIdentity(ut.getUserId());
        Result result = new Result();
        result.setResult(memberIdentityResponse);
        return result;
    }

}
